//
//  PickerViewController.m
//  helloword
//
//  Created by chen on 17/2/25.
//  Copyright © 2017年 chen. All rights reserved.
//

#import "PickerViewController.h"

@interface PickerViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
@property (strong, nonatomic) IBOutlet UIPickerView *pickerView;
@property (strong, nonatomic) IBOutlet UILabel *label;
//保存全部数据
@property (strong,nonatomic) NSDictionary *pickerData;
//当前的省数据
@property (strong,nonatomic) NSArray *pickerProvincesData;
//当前省下面的市数据
@property (strong,nonatomic) NSArray *pickerCitiesData;

@end

@implementation PickerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initProvincesData];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)initProvincesData{
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *plistPath = [bundle pathForResource:@"province_city" ofType:@"plist"];
    //获取属性列表文件中的全部数据
    NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfFile:plistPath];
    _pickerData = dict;
    //省份名数据
    _pickerProvincesData = [_pickerData allKeys];
    //默认取出第一个省的所有市的数据
    NSString *selectProvinces = [_pickerProvincesData objectAtIndex:0];
    _pickerCitiesData = [_pickerData objectForKey:selectProvinces];

}
- (IBAction)onClick:(id)sender {
   
    NSInteger row1 = [_pickerView selectedRowInComponent:0];
    NSInteger row2 = [_pickerView selectedRowInComponent:1];
    NSString *selected1 = [_pickerProvincesData objectAtIndex:row1];
    NSString *selected2 = [_pickerCitiesData objectAtIndex:row2];
    NSString *title = [[NSString alloc]initWithFormat:@"%@,%@市",selected1,selected2 ];
    _label.text = title;
}

#pragma mark --实现协议UIPickerViewDataSource方法
//Components表示为列，第一列为0是省份，第二列为1是城市
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2;
}
//返回某一列的行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component ==0) {
        return [_pickerProvincesData count];
    }else{
        return [_pickerCitiesData count];
    }
}
#pragma mark --实现协议UIPickerViewDelegate方法
//返回某一列的行元素
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == 0) {
        return [_pickerProvincesData objectAtIndex:row];
    }else{
        return [_pickerCitiesData objectAtIndex:row];
    }
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component == 0) {
        NSString *selectedProvince = [_pickerProvincesData objectAtIndex:row];
        NSArray *array = [_pickerData objectForKey:selectedProvince];
        _pickerCitiesData = array;
        [_pickerView reloadComponent:1];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
